const FLY_EFFECT_PREFAB = "prefabs/ui/FlyEffect";   //FlyEffect的prefab动态加载路径
const POOL_SIZE = 20;
let _flyEffectPool = null;
let _flyEffectPrefab = null;
let FlyEffect = cc.Class({
  extends: cc.Component,
  properties: {
    defaultIconSize: new cc.Size(90, 90),
    // icon1: cc.Sprite,
    // icon2: cc.Sprite,
    // icon3: cc.Sprite,
  },
  
  ctor() {
    this.iconSprites = [];
  },
  
  onLoad() {
    this.initFlyEffect();
  },
  
  initFlyEffect() {
    _flyEffectPool = new cc.NodePool();
    for (let i = 0; i < this.poolSize; i++) {
      let itemNode = cc.instantiate(this.itemPrefab);
      _flyEffectPool.put(itemNode);
    }
    this.iconSprites = [];
    for (let i = 0; i < 3; i++) {
      this.creatIconSprite();
    }
  },
  
  creatIconSprite() {
    let iconNode = new cc.Node();
    iconNode.parent = this.node;
    let iconSprite = iconNode.addComponent(cc.Sprite);
    this.iconSprites.push(iconSprite);
    return iconSprite;
  },
  
  play(worldPos, targetNode, spriteFrame, count, size) {
    count = count === undefined ? 1 : count;
    size = size === undefined ? this.defaultIconSize : size;
    this.node.setSiblingIndex(cc.macro.MAX_ZINDEX);
    this.node.position = this.node.parent.convertToNodeSpaceAR(worldPos);
    
    
    let actScaleTo = cc.scaleTo(0.4 / 3, 1);
    worldPos = targetNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
    let actMoveTo = cc.moveTo(1 / 3, this.node.convertToNodeSpaceAR(worldPos));
    
    let actDelay = cc.delayTime(0.2 / 3);
    let actFadeOut = cc.fadeOut(0.4 / 3);
    
    for (let i = 0; i < count; i++) {
      let iconSprite;
      if (i < this.iconSprites.length) {
        iconSprite = this.iconSprites[i];
      } else {
        iconSprite = this.creatIconSprite();
      }
      iconSprite.spriteFrame = spriteFrame;
      iconSprite.node.active = true;
      iconSprite.node.opacity = 255;
      iconSprite.node.scaleX = 0;
      iconSprite.node.position = cc.Vec2.ZERO;
      iconSprite.node.setContentSize(size);
      
      iconSprite.node.runAction(cc.sequence(cc.delayTime(0.15 * i), actScaleTo.clone(), actMoveTo.clone(), actDelay.clone(), actFadeOut.clone()));
      this.scheduleOnce(() => {
        if (_flyEffectPool.size() < 5)
          _flyEffectPool.put(this.node);
      }, 1);
    }
    for (let i = count; i < this.iconSprites.length; i++) {
      let iconSprite = this.iconSprites[i];
      iconSprite.node.active = true;
    }
    
  }
});

function _doPlay(fromNodeOrWorldPos, targetNode, spriteFrame, count, size) {
  let worldPos = fromNodeOrWorldPos instanceof cc.Vec2 ? fromNodeOrWorldPos : fromNodeOrWorldPos.convertToWorldSpaceAR(cc.Vec2.ZERO);
  let itemNode;
  if (_flyEffectPool.size() > 0) {
    itemNode = _flyEffectPool.get();
  } else if (_flyEffectPrefab) {
    itemNode = cc.instantiate(_flyEffectPrefab);
  }
  if (itemNode) {
    itemNode.parent = cc.director.getScene();
    itemNode.zIndex = cc.macro.MAX_ZINDEX;
    let flyEffect = itemNode.getComponent(FlyEffect);
    flyEffect.play(worldPos, targetNode, spriteFrame, count, size);
  }
  
}

FlyEffect.init = function (callback) {
  if (_flyEffectPool) {
    if (callback) {
      callback();
    }
    return;
  }
  
  _flyEffectPool = new cc.NodePool();
  cc.loader.loadRes(FLY_EFFECT_PREFAB, cc.Prefab, function (err, prefab) {
    _flyEffectPrefab = prefab;
    for (let i = 0; i < POOL_SIZE; i++) {
      let itemNode = cc.instantiate(prefab);
      _flyEffectPool.put(itemNode);
    }
    if (callback) {
      callback();
    }
  });
};


FlyEffect.play = function (fromNodeOrWorldPos, targetNode, spriteFrame, count, size) {
  if (!fromNodeOrWorldPos)
    return;
  if (_flyEffectPool) {
    _doPlay(fromNodeOrWorldPos, targetNode, spriteFrame, count, size);
  } else {
    FlyEffect.init(() => {
      _doPlay(fromNodeOrWorldPos, targetNode, spriteFrame, count, size);
    })
  }
};

module.exports = FlyEffect;





